package com.diapoc.androidteacher.ColorProgressBar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.text.Layout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;

public class ColorProgressBar extends View implements OnTouchListener,
		OnClickListener {

	public int height = 0;
	public int width = 0;
	public int height_mode;
	public int width_mode;

	public ColorProgressBar(Context context) {
		super(context);
		init();
	}

	public ColorProgressBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public ColorProgressBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public void init() {
		Log.v("test", "init");
		this.setOnClickListener(this);
		initPaint();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Log.v("test", "ondraw");
		super.onDraw(canvas);
		drawCanvas(canvas);
		// switch (width_mode) {
		// case MeasureSpec.AT_MOST:
		// Log.v("test", "AT_MOST");
		// break;
		// case MeasureSpec.EXACTLY:
		// Log.v("test", "EXACTLY");
		// break;
		// case MeasureSpec.UNSPECIFIED:
		// Log.v("test", "UNSPECIFIED");
		// break;
		// }
		// Log.v("test", toString());
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		width = MeasureSpec.getSize(widthMeasureSpec);
		height = MeasureSpec.getSize(heightMeasureSpec);
		width_mode = MeasureSpec.getMode(widthMeasureSpec);
		height_mode = MeasureSpec.getMode(heightMeasureSpec);
	}

	@Override
	public String toString() {
		return "TimeView [height=" + height + ", width=" + width
				+ ", height_mode=" + height_mode + ", width_mode=" + width_mode
				+ "]";
	}

	@Override
	public void onClick(View v) {
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		return false;
	}

	// 自定义界面
	public static final int STATE_INIT = 0x0000;
	public static final int STATE_START = 0x0001;
	public static final int STATE_PAUSE = 0x0002;
	public static final int STATE_FINISH = 0x0003;

	int rate = 30; // 进度
	int state; // 状态
	public Paint paintStroke;
	public Paint paintInner;
	public TextPaint paintText;
	String text = "开始下载";
	int strokeWidth = 10;

	private void initPaint() {
		paintStroke = new Paint();
		paintStroke.setStyle(Style.STROKE);
		paintStroke.setStrokeWidth(strokeWidth);

		paintInner = new Paint();
		paintText = new TextPaint();
		paintText.setTextSize(30);
		paintText.setColor(Color.WHITE);

	}

	public void drawCanvas(Canvas canvas) {
		Log.v("test", rate + "%");
		// canvas.drawRect(0, 0, width, height, paintStroke);
		switch (state) {
		case STATE_INIT:
			paintInner.setColor(Color.rgb(72, 176, 237));
			break;
		case STATE_START:
			paintInner.setColor(Color.rgb(14, 204, 56));
			break;
		case STATE_PAUSE:
			paintInner.setColor(Color.rgb(237, 71, 71));
			break;
		case STATE_FINISH:
			paintInner.setColor(Color.rgb(225, 128, 193));
			break;
		}
		canvas.drawRect(strokeWidth / 2, strokeWidth / 2, width * rate / 100
				- strokeWidth / 2, height - strokeWidth / 2, paintInner);
		canvas.drawText(text, (width - getTextWidth(text, paintText)) / 2,
				(height + getTextHeight(paintText)) / 2, paintText);
	}

	public void setState(int state) {
		this.state = state;
		postInvalidate();
	}

	public void setRate(int rate) {
		this.rate = rate;
		postInvalidate();
	}

	public void setText(String text) {
		this.text = text;
		postInvalidate();
	}

	public float getTextWidth(String string, TextPaint paint) {
		return Layout.getDesiredWidth(string, paint);
	}

	public float getTextHeight(TextPaint paint) {
		return (-paint.getFontMetrics().top) / 1.5f;
	}

}
